package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_dialog

import android.content.Context
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.TextView
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import com.bp.hearthealth.pulse.lnapps.bp_view.bp_number_picker.BPNumberPicker
import com.google.android.material.bottomsheet.BottomSheetDialog
import java.util.Calendar

class BpTimerPickerDialog(
    context: Context,
    type: String,
    titleStr: String,
    monthList: Array<String>,
    callback: TimePickerCallback
) {
    private val bottomDialog: BottomSheetDialog
    private lateinit var year: BPNumberPicker
    private lateinit var month: BPNumberPicker
    private lateinit var title: TextView
    private lateinit var day: BPNumberPicker
    private lateinit var hour: BPNumberPicker
    private lateinit var minute: BPNumberPicker
    private val isDateTime: Boolean


    interface TimePickerCallback {
        fun setDate(
            year: String,
            month: String,
            day: String,
            hour: String,
            minute: String,
            type: String
        )
    }

    init {
        bottomDialog = getBottomDialog(context, R.layout.view_time_picker_bg)
        val calendar: Calendar = Calendar.getInstance()

        // 根据传入的类型确定显示日期部分还是时间部分
        isDateTime = type == Constants.TYPE_DATE || type == Constants.TYPE_BIRTHDAY
        title = bottomDialog.findViewById(R.id.title)!!
        title.text = titleStr
        if (isDateTime) {
            // 设置日期选择器
            bottomDialog.findViewById<View>(R.id.date_container)?.visibility = View.VISIBLE
            bottomDialog.findViewById<View>(R.id.time_container)?.visibility = View.GONE

            val yearNow: Int = calendar.get(Calendar.YEAR)
            year = bottomDialog.findViewById<View>(R.id.year) as BPNumberPicker
            year.run {
                minValue = if (type == Constants.TYPE_BIRTHDAY) {
                    1980
                } else {
                    2005
                }
                maxValue = yearNow
                value = yearNow
            }

            month = bottomDialog.findViewById<View>(R.id.month) as BPNumberPicker
            month.run {
                minValue = 1
                maxValue = monthList.size
//                val calendar = Calendar.getInstance()
//                val currentMonth = calendar[Calendar.MONTH] + 1
                maxValue = maxValue
                displayedValues = monthList
                value = calendar.get(Calendar.MONTH) + 1
            }

            day = bottomDialog.findViewById<View>(R.id.day) as BPNumberPicker
            val days: Int = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
            val newDays = getNewDays(days)
            day.run {
                minValue = 1
                maxValue = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
                displayedValues = newDays
                value = calendar.get(Calendar.DAY_OF_MONTH)
            }
        } else {
            // 设置时间选择器
            bottomDialog.findViewById<View>(R.id.date_container)?.visibility = View.GONE
            bottomDialog.findViewById<View>(R.id.time_container)?.visibility = View.VISIBLE

            hour = bottomDialog.findViewById<View>(R.id.hour) as BPNumberPicker
            hour.run {
                minValue = 0
                maxValue = 23
                value = calendar.get(Calendar.HOUR_OF_DAY)
                setFormatter { value -> if (value < 10) "0$value" else value.toString() }
            }

            minute = bottomDialog.findViewById<View>(R.id.minute) as BPNumberPicker
            minute.run {
                minValue = 0
                maxValue = 59
                value = calendar.get(Calendar.MINUTE)
                setFormatter { value -> if (value < 10) "0$value" else value.toString() }
            }
        }

        val ok = bottomDialog.findViewById<View>(R.id.ok)
        ok?.setOnClickListener { v: View? ->
            bottomDialog.dismiss()
            if (isDateTime) {
                val curMonth = month.value
                val monthValue = if (curMonth < 10) "0${curMonth}" else curMonth.toString()

                val curDay = day.value
                val dayValue = if (curDay < 10) "0${curDay}" else curDay.toString()
                callback.setDate(year.value.toString(), monthValue, dayValue, "", "", type)
            } else {
                val curHour = hour.value
                val hourValue = if (curHour < 10) "0${curHour}" else curHour.toString()

                val curDay = minute.value
                val monthValue = if (curDay < 10) "0${curDay}" else curDay.toString()
                callback.setDate("", "", "", hourValue, monthValue, type)
            }
        }
    }

    private fun getBottomDialog(context: Context, id: Int): BottomSheetDialog {
        val bottomSheet = BottomSheetDialog(context)
        bottomSheet.setContentView(id)
        bottomSheet.setCancelable(true)
        val bottom =
            bottomSheet.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
        if (bottom != null) {
            bottom.setBackgroundResource(R.color.transparent)
            val layoutParams = bottom.layoutParams
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            bottom.layoutParams = layoutParams
        }
        return bottomSheet
    }

    private fun dpToPx(context: Context, dp: Float): Int {
        return (dp * context.resources.displayMetrics.density + 0.5).toInt()
    }

    fun show() {
        bottomDialog.show()
    }

    fun setYearValue(yearValue: Int) {
        year.value = yearValue
        updateNumberOfDays()
    }

    fun setMonthValue(monthValue: Int) {
        month.value = monthValue
        updateNumberOfDays()
    }

    fun setDayValue(dayValue: Int) {
        day.value = dayValue
    }

    private fun updateNumberOfDays() {
        val calendar: Calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR, year.value)
        calendar.set(Calendar.MONTH, month.value - 1)
        calendar.set(Calendar.DATE, 1)
        calendar.roll(Calendar.DATE, -1)
        val date: Int = calendar.get(Calendar.DATE)
        day.maxValue = date
        day.displayedValues = getNewDays(date)
    }

    private fun getNewDays(days: Int): Array<String> {
        val dayList: MutableList<String> = ArrayList()
        for (i in 0 until days) {
            if (i + 1 < 10) {
                dayList.add("0" + (i + 1))
            } else {
                dayList.add((i + 1).toString())
            }
        }
        return dayList.toTypedArray()
    }
}
